/*
 * 路由组件的处理方法
 * @Author: ljb
 * @Date: 2018-11-09 16:48:29
 * @Last Modified by: kanglin
 * @Last Modified time: 2018-11-22 18:15:49
 */
/* eslint-disable-next-line import/extensions */

import Toast from 'cube-ui/lib/toast';
import { $t } from 'src/i18n';
import Util from './libs/util';

let loading_toast = null;

/**
 * 开启loading
 */
function startLoading() {
	const t = Toast.$create({
		time: 0,
		txt: $t('common.wait'),
	});
	t.show();
	loading_toast = t;
}

function closeLoading() {
	loading_toast.hide();
}

// 路由处理方法
const handleRouter = {
	// 路由切换前的处理
	before(to, from, next) {
		// reset title
		startLoading();
		Util.title(to.meta.title);
		return next();
	},
	// 路由切换后的处理
	after() {
		// Vue.Toast.$loading.close();
		closeLoading();
		window.scrollTo(0, 0);
	},
};

export default handleRouter;
